/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isvp.utilities;

import isvp.constants.ExceptionConstants;
import isvp.constants.ISVPConstants;
import isvp.exceptions.ComponentException;
import isvp.interfaces.ISVP;
import isvp.sax.Saxer;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * Utilities 
 * @author jjophin
 */
public class ISVPUtils {
    
    /**
     * Returns and input stream of the entry corresponding to componentFileName from the isvpFile
     * @param isvpFile - Instance of a class implementing the ISVP interface
     * @param componentFileName - Name of the file to be retrieved from the ISVP
     * @param componentName - Name of the component that the file represents
     * @return InputStream instance corresponding to the retrieved entry
     * @throws ComponentException if no entry is found corresponding to the componentFileName in the isvpFile
     */
    private static InputStream getEntryStream(ISVP isvpFile, String componentFileName, String componentName) throws ComponentException {
        InputStream iStream = null;
        ZipEntry entry = isvpFile.getEntry(componentFileName);
        if(entry == null)
            throw new ComponentException(ExceptionConstants.COMP_NO_FILE_EXCEPTION, componentName);
        try {
            iStream = isvpFile.getInputStream(entry);
        } catch (IOException ex) {
            throw new ComponentException(ExceptionConstants.COMP_FILE_READ_EXCEPTION, componentName);
        }
        return iStream;
    }
    
    /**
     * Returns the name of the xml file in the ISVP that contains the details of the specified component
     * @param isvpFile : ISVP Object representing the ISVP file
     * @param componentName : Name of the component whose filename is to be fetched.
     * @return Name of the xml file corresponding to the component in the ISVP
     * @throws ComponentException if the component has not been included in the ISVP
     */
    public static String getComponentFileName(ISVP isvpFile, String componentName) throws ComponentException{
        Document currentISVXML = isvpFile.getIsvXML();
        String componentFileXPATH = ISVPConstants.getXPath(componentName);
        String file = XMLUtils.getTextContent(currentISVXML, componentFileXPATH);
        if(file.length() == 0) {
            throw new ComponentException(ExceptionConstants.COMP_NO_COMPONENT_EXCEPTION, componentName);
        }
        return file;
    }
    
    /**
     * Get the XML content of the component from the ISVP
     * @param componentFileName : Name of the component xml file in the ISVP
     * @return : xml content in the corresponding file
     * @throws ComponentException : if the file is not found
     */
    public static Document getComponentDocument(ISVP isvpFile, String componentFileName, String componentName) throws ComponentException {
        InputStream iStream = getEntryStream(isvpFile, componentFileName, componentName); 
        //PrintClass.printXML(iStream);
        Document document = null;
        try {
            document = XMLUtils.getXMLDocument(iStream);
        } catch (SAXException ex) {
            throw  new ComponentException(ExceptionConstants.COMP_INVALID_XML_EXCEPTION, componentName);
        } finally {
            try {
                iStream.close();
            } catch (IOException ex) {
            }
        }
        //PrintClass.printXML(document);
        return document;
    }
    
    /**
     * To get the minimized XML of a component as governed by the handler for that component in isvp.sax.handlers 
     * @param isvpFile
     * @param componentFileName
     * @param componentName
     * @return
     * @throws ComponentException 
     */
    public static Document getSAXedComponentDocument(ISVP isvpFile, String componentFileName, String componentName) throws ComponentException {
        InputStream iStream = getEntryStream(isvpFile, componentFileName, componentName);        
        Document doc;
        try {
            doc = Saxer.parse(iStream, componentName);
        } catch (SAXException ex) {
            throw  new ComponentException(ExceptionConstants.COMP_INVALID_XML_EXCEPTION, componentName);
        } catch (IOException ex) {
            throw new ComponentException(ExceptionConstants.COMP_FILE_READ_EXCEPTION, componentName);
        }
        return doc;
    }
}
